package com.xiaozhengkeji.mcverifyplus.pojo;


import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import com.google.gson.Gson;


import com.xiaozhengkeji.mcverifyplus.pojo.type.ResCodeType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;


@Data
@Slf4j
@ApiModel(value = "返回结构体", description = "返回结构体")
public class ResponseBean {
    public static Sign SIGN = SecureUtil.sign(SignAlgorithm.SHA256withRSA);
    //Http状态码
    @ApiModelProperty(value = "返回结果码")
    private Integer code;
    //Http状态信息
    @ApiModelProperty(value = "返回结果简介")
    private String code_name;
    //返回信息
    @ApiModelProperty(value = "返回结果")
    private String msg;
    //返回的数据
    @ApiModelProperty(value = "返回结构体数据")
    private Object data;
    //返回的数据
    @ApiModelProperty(value = "返回结构体数据 - 备用")
    private Object data_standby;
    //返回数据的签名
    @ApiModelProperty(value = "返回结构体数据签名数据")
    private String sig;

    public ResponseBean(ResCodeType code, String msg, Object data, Object dataStandby) {
        this.code = code.getSTATUS_CODE();
        this.code_name = code.getSTATUS_NAME();
        this.msg = msg;
        this.data = data;
        this.data_standby = dataStandby;
        this.sig = Base64.encode(SIGN.sign(new Gson().toJson(data).getBytes()));
    }

    /**
     * 验证签名正确性
     */
    public static Boolean verifySignature(String data, String signed) {
        return SIGN.verify(data.getBytes(), signed.getBytes());
    }
}
